﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.DataVisualization.Charting;
using System.Data;
using System.Drawing;

using GroundWater.Data;
using GroundWater.BusinessActions;

using MonitoringTable = GroundWater.Data.MonitoringDataSet.MonitoringDataTable;
using MonitoringRow = GroundWater.Data.MonitoringDataSet.MonitoringRow;

using WellTable = GroundWater.Data.InfoDataSet.WellsDataTable;
using WellRow = GroundWater.Data.InfoDataSet.WellsRow;


namespace GroundWater.waterlevel
{
    public partial class waterlevelchart : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["charttype"] == "pie")
            {
                SiteUtil.Validate(this, "水位饼状图");
            }

            if (Request.QueryString["charttype"] == "column")
            {
                SiteUtil.Validate(this, "水位柱状图");
            }

            if (!this.IsPostBack)
            {
                //DropDownList1.DataSource = Enum.GetNames(typeof(SeriesChartType));
                //DropDownList1.DataBind();

                txtStartDate.Value = DateTime.Now.ToString("yyyy-MM-dd");

                //PopulateSeriesChartType();
                //if (Request.QueryString["charttype"] == "pie")
                //{
                //    drpChartType.SelectedIndex = 1;
                //}

                //drpSectionPlane.DataSource = Monitoring.FetchSectionPlane();
                //drpSectionPlane.DataTextField = "SectionPlane";
                drpSectionPlane.DataBind();
                
                //int i = drpSectionPlane.Controls.Count;
                if (drpSectionPlane.Items.Count > 0)
                {
                    GenerateImage();
                }

                
            }
        }



        protected void drpSectionPlane_SelectedIndexChanged(object sender, EventArgs e)
        {
            GenerateImage();
        }

        protected void drpChartType_SelectedIndexChanged(object sender, EventArgs e)
        {
            GenerateImage();
        }

        protected void drp3D_SelectedIndexChanged(object sender, EventArgs e)
        {
            GenerateImage();
        }

        private void GenerateImage(Chart chart, SeriesChartType chartType)
        {
            chart.Series["Series1"].ChartType = chartType;
            chart.Series["Series1"]["DrawingStyle"] = "Emboss";
            //chart.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
            chart.Series["Series1"].IsValueShownAsLabel = true;

            ProductsDataObject products = new ProductsDataObject();
            DataView view = products.Select();
            view.RowFilter = "ProductID < 10";

            chart.DataSource = view;
            chart.Series["Series1"].XValueMember = "ProductID";
            chart.Series["Series1"].YValueMembers = "UnitsInStock";
            //chart.Series["Series1"].AxisLabel = "ProductName";
            //chart.Series["Series1"].Points
            DataPoint p = new DataPoint(12, 30);
            p.AxisLabel = "AxisLabel";
            p.Label = "Label";

            chart.DataBind();

            chart.Series["Series1"].Points.Add(p);
        }

        //private void PopulateSeriesChartType()
        //{
        //    //SeriesChartType.Point
        //    //SeriesChartType.Line
        //    //SeriesChartType.Pie
        //    //SeriesChartType.Column
        //    //SeriesChartType.Spline
        //    ListItem column = new ListItem("柱状图", "Column");
        //    //ListItem line = new ListItem("折线图", "Line");
        //    //ListItem spline = new ListItem("曲线图", "Spline");
        //    ListItem pie = new ListItem("饼图", "Pie");
        //    //ListItem point = new ListItem("点状图", "Point");

        //    DropDownList drp = drpChartType;
        //    drp.Items.Add(column);
        //    //drp.Items.Add(line);
        //    //drp.Items.Add(spline);
        //    drp.Items.Add(pie);
        //    //drp.Items.Add(point);
        //}

        protected void btnGenerate_Click(object sender, EventArgs e)
        {
            GenerateImage();
        }

        private void GenerateImage()
        {
            if (drpSectionPlane.SelectedItem == null)
                return;
            DataTable dt = Monitoring.FetchWaterLeveGroupByWellCode(drpSectionPlane.SelectedItem.Text, txtStartDate.Value, DateTime.Parse(txtStartDate.Value).AddDays(1).ToString("yyyy-MM-dd"));
            WellTable wellTable = Well.GetAll(false, true);
            var wells = wellTable.AsEnumerable();
            var avgData = dt.AsEnumerable();

            var q = from w in wells
                    join avg in avgData on w.Code equals avg["WellCode"]
                    orderby w.SequenceNumber
                    select new { WellCode = w.Code, Waterlevel = (decimal)avg["Waterlevel"] };

            var waterlevels = q.AsEnumerable();
            

            
            Chart chart = WaterLevelChart;
            int width = 612;
            int height = 396;
            if(Husb.Util.CommonUtil.IsInteger(txtWidth.Value))
            {
                width = Int32.Parse(txtWidth.Value);
            }
            if (Husb.Util.CommonUtil.IsInteger(txtHeight.Value))
            {
                height = Int32.Parse(txtHeight.Value);
            }
            chart.Width = new Unit(width);
            chart.Height = new Unit(height);

            //SeriesChartType chartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), drpChartType.SelectedItem.Value, true);

            SeriesChartType chartType = Request.QueryString["charttype"] == "pie" ? SeriesChartType.Pie : SeriesChartType.Column;

            Series series = chart.Series["Series1"];
            series.ChartType = chartType;
            series["DrawingStyle"] = "Emboss";//Emboss
            //if (drp3D.SelectedIndex == 1)
            //{
            //    chart.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
            //    series["DrawingStyle"] = "Cylinder";
            //}
            series.IsValueShownAsLabel = true;

            chart.Titles["Title1"].Text = drpSectionPlane.SelectedItem.Text + " 水井的水位对比图";
            chart.ChartAreas["ChartArea1"].AxisY.Title = "水位 单位：米";
            chart.ChartAreas["ChartArea1"].AxisY.TitleFont = new System.Drawing.Font(FontFamily.GenericSansSerif, 12, FontStyle.Bold);

            int i = 1;
            foreach (var avg in waterlevels)
            {
                string wellCode = avg.WellCode;
                decimal waterlevel = avg.Waterlevel;
                double x = Convert.ToDouble(i);
                double y = Convert.ToDouble(waterlevel);

                DataPoint p = new DataPoint(x, y);
                p.AxisLabel = wellCode.Substring(wellCode.Length - 3, 3) + "#";
                p.Label = Math.Round(y, 2).ToString();

                series.Points.Add(p);
                i++;
            }
            //foreach (DataRow row in dt.Rows)
            //{
            //    string wellCode = (string)row["WellCode"];
            //    decimal waterlevel = (decimal)row["Waterlevel"];
            //    double x = Convert.ToDouble(i);
            //    double y = Convert.ToDouble(waterlevel);

            //    DataPoint p = new DataPoint(x, y);
            //    p.AxisLabel = wellCode.Substring(wellCode.Length - 3, 3) + "#";
            //    p.Label = Math.Round(y, 2).ToString();

            //    series.Points.Add(p);
            //    i++;
            //}
        }

        protected void drpSectionPlane_DataBound(object sender, EventArgs e)
        {
            string id = Request.QueryString["id"];
            if (!String.IsNullOrEmpty(id))
            {
                WellRow row = GroundWater.BusinessActions.Well.GetById(id, false);

                //SetFields(id);
                foreach (ListItem item in drpSectionPlane.Items)
                {
                    if (item.Text == row.SectionPlane)
                    {
                        item.Selected = true;
                        break;
                    }
                }

            }
        }

    }
}
